package net.homelinux.ajay.net.ondisk.view.dialogs.client;

import net.homelinux.ajay.net.ondisk.model.client.Link;
import net.homelinux.ajay.net.ondisk.model.client.MetaFactory;
import net.homelinux.ajay.net.ondisk.view.client.AppTree;
import net.homelinux.ajay.net.ondisk.view.client.FolderTreeItem;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class AddLinksDialog extends AppDialogBox {

  private TextBox urlbox;
  private Button addButton;
  private int oldlength;
  private int draggedLinksAction;

  /**
   * 
   */
  public AddLinksDialog() {
    this(false);
  }

  /**
   * @param autoHide
   */
  public AddLinksDialog(boolean autoHide) {
    super(autoHide);
    init();
  }

  public String getValue() {
    // TODO Auto-generated method stub
    return null;
  }
  
  private void init(){

    FolderTreeItem parent = (FolderTreeItem) AppTree.getTree().getSelectedItem();
    setPopupTop( parent.getAbsoluteTop()+parent.getOffsetHeight());
    setText("Add Folder to : "+ parent.getFolder().getName());

    final Panel dragTarget = new FlowPanel();
    
    dragTarget.add(new Label("For the links dragged below:"));
    
    final ListBox choice = new ListBox();
    choice.addItem("Leave for editing","0");
    choice.addItem("Get size data","1");
    choice.addItem("Add to collection","2");
    choice.addChangeListener(new ChangeListener(){

      public void onChange(Widget sender) {
        setDraggedLinksAction(choice.getSelectedIndex());
        
      }});
    
    dragTarget.add(choice);

    dragTarget.add(new Label("Drag link or type URL:"));
    
    urlbox = new TextBox(){
      public void onBrowserEvent(Event event){
        onEditChange();
        super.onBrowserEvent(event);
        DOM.eventCancelBubble(event, true);
      }
    };
    
    urlbox.sinkEvents(Event.ONCHANGE|Event.MOUSEEVENTS|Event.FOCUSEVENTS);
    
    dragTarget.add(urlbox);
    
    
    addButton = new Button("Add to collection");
    addButton.addClickListener(new ClickListener(){

      public void onClick(Widget sender) {
        if(sender==addButton){
          addUrl2Collection();
        }
      }
      });
    dragTarget.add(addButton);
    
    dragTarget.add(new OkCancelPanel(this){

      public void onCancel() {
        dismiss();
      }

      public void onOk() {
        dismiss();
      }

      });
//    treepanel.add(dragTarget);
    
//    this.dragPanel = dragTarget;
    
    setWidget(dragTarget);
    
    
  }


  protected void dismiss() {
    hide();
  }

  private void addToCollection() {
    // TODO Auto-generated method stub
    System.out.println("adding the above to collection");
    
  }

 protected void addUrl2Collection() {
    FolderTreeItem parent = (FolderTreeItem) AppTree.getTree().getSelectedItem();
    if(parent!=null){
      parent.makeLinkChild(urlbox.getText());
    }
    
  }

  protected void onEditChange() {
    String text = urlbox.getText();
    if(text.length()>oldlength){
      oldlength = text.length()+1;
      //urlbox.setText("");
      //urlbox2.setText(text);
      if(draggedLinksAction>0)
        processUrl(text);
      if(draggedLinksAction>1)
        urlbox.setText("");
        addToCollection();
      
    }else{ 
      oldlength = text.length()+1;
    }
   
  }
  
  private void processUrl(String url) {
    System.out.println(url);
    final Link meta = MetaFactory.getInstance().getMeta(url);
    Timer t = new Timer(){

      public void run() {
        if(meta.getHttpLoadStatus()>=Link.LOADED){
          this.cancel();
          processLoadedMeta(meta);
        }
      }

       
    };
    t.scheduleRepeating(1000);
  }
  private void processLoadedMeta(Link meta) {
    // TODO Auto-generated method stub
    
  }

  private void setDraggedLinksAction(int ind) {
    draggedLinksAction = ind;

  }

  public static AddLinksDialog makeAddLinksDialog() {
    return new AddLinksDialog();
  }

}
